package semantic.analyzer;

import metamodel.PrimitiveTypeEnum;
import semantic.model.ExpressionNode;
import semantic.model.UndefinedExpressionNode;

public class NumericExpressionAnalyzer {

	public static void checkType(ExpressionNode... nodes) throws Exception {
		for (ExpressionNode node : nodes) {
			if (node != null && node.getType().equalsIgnoreCase(PrimitiveTypeEnum.UNDEFINED.name())) {
				UndefinedExpressionNode uNode = (UndefinedExpressionNode) node;
				uNode.setTypeItShouldBe(PrimitiveTypeEnum.REAL.name());
			} else if (node != null && !(isInteger(node) || isReal(node))) {
				throw new Exception("Era esperado tipo numérico!");
			}
		}
	}
	
	public static boolean checkIfItsNumeric(String type) {
		return type.equalsIgnoreCase(PrimitiveTypeEnum.REAL.name()) || type.equalsIgnoreCase(PrimitiveTypeEnum.INTEGER.name());
	}

	private static boolean isReal(ExpressionNode node) {
		return node.getType().equalsIgnoreCase(PrimitiveTypeEnum.REAL.name());
	}

	private static boolean isInteger(ExpressionNode node) {
		return node.getType().equalsIgnoreCase(PrimitiveTypeEnum.INTEGER.name());
	}

}
